package com.example.memolaga;

import android.content.Context;
import android.util.Log;
import android.widget.Toast;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class Functions {
	
	public static boolean partialAnd(boolean cond1, boolean cond2)
	{
		if (cond1) 
			return cond2;
		return cond1;
	}

	public static void showShortToast(Context con, String str)
	{
		Toast toast = Toast.makeText(con, str, Toast.LENGTH_SHORT);
		toast.show();
	}
	
	public static void showLongToast(Context con, String str)
	{
		Toast toast = Toast.makeText(con, str, Toast.LENGTH_LONG);
		toast.show();
	}

    public static int parseString(String s)
    {
        return Integer.getInteger(s);
    }

    public static String getStringOfFile(String path)
    {
        String ret = null;
        Log.i("Functions getStringOfFile", "File: "+ path);
        try{
            StringBuffer fileData = new StringBuffer(1000);
            BufferedReader reader = new BufferedReader(new FileReader(path));
            char[] buf = new char[1024];
            int numRead=0;
            while((numRead=reader.read(buf)) != -1){
                String readData = String.valueOf(buf, 0, numRead);
                fileData.append(readData);
                buf = new char[1024];
            }
            reader.close();
            ret = fileData.toString();
        }
        catch(IOException e)
        {
            Log.e("getStringOfFile e", e.getMessage());
        }
        return ret;
    }

    public static boolean isOkCardNumber(String cardNumber)
    {
        if (cardNumber.length()!=16) return false;
        int pari=0, dispari=0;
        char[] number = cardNumber.toCharArray();
        for (int i=0;i<16;i++)
            if (i%2==0) {
                int p= Character.getNumericValue(number[i])*2;
                //int p=Integer.valueOf(number[i]) *2;
                if (p>9)
                    pari =pari+p%10+p/10;
                else
                    pari = pari+p;
            }
            else
                dispari = dispari + Character.getNumericValue(number[i]);
        if (((pari+dispari)%10)!=0) return false;
        return true;
    }


    public static boolean isOkCVV(String cvv)
    {
        if (cvv.length()==3) return true;
        return false;
    }

    public static boolean isOkPin(String pin)
    {
        if (pin.length()==5) return true;
        return false;
    }
}
